import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics

# 1. 读取数据
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')

# 2. 数据预处理（根据实际数据字段调整）
# 假设 'target' 为标签列，其余都是特征
y = train['target']
X = train.drop('target', axis=1)
X_test = test.copy()

# 如有缺失值，可简单填充
X = X.fillna(X.median())
X_test = X_test.fillna(X_test.median())

# 如有类别特征，可编码（此处假设无）
# 如果有类别特征，比如 'Sex', 可加：
# X['Sex'] = X['Sex'].map({'male': 0, 'female': 1})
# X_test['Sex'] = X_test['Sex'].map({'male': 0, 'female': 1})

# 3. 划分训练与验证集
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# 4. 训练模型（随机森林可替换为逻辑回归等其他模型）
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)

# 5. 验证集评估：AUC
y_val_pred = clf.predict_proba(X_val)[:,1]
auc = metrics.roc_auc_score(y_val, y_val_pred)
print(f"Validation AUC: {auc:.4f}")

# 6. 测试集预测（概率）
y_test_pred = clf.predict_proba(X_test)[:,1]

# 7. 生成提交文件
# 假设 test.csv 中有 'Id' 或类似唯一标识
submission = pd.DataFrame({
    'Id': test['Id'],
    'Prediction': y_test_pred
})
submission.to_csv('submission.csv', index=False)
print('Submission file saved as submission.csv') 